package com.flute.framework.data.preparer;

import com.flute.framework.FluteMonitor;
import com.flute.framework.data.mysql.MySQLConnectionUtil;
import com.flute.framework.data.mysql.MysqlDataPreparer;
import com.flute.framework.exception.IndexException;
import com.flute.framework.monitor.EventLogger;
import com.flute.tools.data.IEditor;
import com.flute.tools.util.ExceptionUtil;
import com.flute.tools.util.exceptoin.DataParserException;

public class FileDataPreparer {
	private String filePreparerName;
	private MysqlDataPreparer preparer;
	private String tableName;

	public String getFilePreparerName() {
		return filePreparerName;
	}

	public void setFilePreparerName(String filePreparerName) {
		this.filePreparerName = filePreparerName;
	}

	public void preparerData() throws IndexException {
		String indexTable = MySQLConnectionUtil.getIndexTable(tableName);
		IEditor indexEditor = MySQLConnectionUtil.getNewMysqlHandler().editor(
				indexTable);

		IEditor historyRecordsEditor = MySQLConnectionUtil.getNewMysqlHandler()
				.editor(MySQLConnectionUtil.getAllRecordsTable(tableName));
		try {
			if (preparer != null) {
				EventLogger.info("把裸数据按照一定的格式录入表'" + indexTable + "'...",
						FluteMonitor.getImporterKey());
				if (preparer.getEditor() == null) {
					preparer.setEditor(indexEditor);
					preparer.setTableName(indexTable);
					preparer.setHistoryEditor(historyRecordsEditor);
				}
				this.preparer.prepare();
				EventLogger.info("数据录入结束.", FluteMonitor.getImporterKey());
			}
		} catch (DataParserException e1) {
			throw new IndexException("Failed to prepare data for table '"
					+ indexTable + "'.\n"
					+ ExceptionUtil.getExceptionContent(e1));
		}
	}
}
